﻿using Se.Common;
using Se.Model.Template.Card;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Se.Model.GameModel.Card
{
    /// <summary>
    /// 牌堆
    /// </summary>
    public class CardDeck
    {
        public CardDeck(CardDeckType type)
        {
            switch (type)
            {
                case CardDeckType.CommonDeck:
                    this.OriginalTemplates = OriginalCardFactory.OriginalCommonTemplates;
                    break;
                case CardDeckType.BossDeck:
                    this.OriginalTemplates = OriginalCardFactory.OriginalBossTemplates;
                    break;
                case CardDeckType.IdentityDeck:
                    this.OriginalTemplates = OriginalCardFactory.OriginalIdentityTemplates;
                    break;
            }
            this.m_Cards = new Queue<CardBaseTemplate>();
        }

        /// <summary>
        /// 初始的牌堆
        /// </summary>
        public readonly List<CardBaseTemplate> OriginalTemplates;

        /// <summary>
        /// 现在牌堆中的牌
        /// </summary>
        private Queue<CardBaseTemplate> m_Cards;

        /// <summary>
        /// 需要的卡牌
        /// </summary>
        /// <param name="number"></param>
        /// <returns></returns>
        public List<CardBaseTemplate> TakeCard(int number)
        {
            List<CardBaseTemplate> cards = new List<CardBaseTemplate>();
            for (var i = 0; i < number; i++)
            {
                if (this.m_Cards.Count == 0)
                {
                    this.ResetCards(cards);
                }
                cards.Add(this.m_Cards.Dequeue());
            }

            return cards;
        }

        /// <summary>
        /// 重置牌堆
        /// </summary>
        public void ResetCards(List<CardBaseTemplate> exceptTemplates)
        {
            var cards = Utility.GetRandomList<CardBaseTemplate>(this.OriginalTemplates);
            this.m_Cards.Clear();
            foreach (var card in cards)
            {
                if (exceptTemplates.Any(c => c.ID == card.ID))
                    continue;
                this.m_Cards.Enqueue(card);
            }
        }
    }

    /// <summary>
    /// 初始的牌集合
    /// </summary>
    public static class OriginalCardFactory
    {
        public static void Init(List<CardBaseTemplate> commonTemplates, List<CardBaseTemplate> identityTemplates, List<CardBaseTemplate> bossTemplates)
        {
            OriginalCommonTemplates = commonTemplates;
            OriginalBossTemplates = bossTemplates;
            OriginalBossTemplates = identityTemplates;
        }

        /// <summary>
        /// 初始的牌堆
        /// </summary>
        public static List<CardBaseTemplate> OriginalCommonTemplates { get; private set; }

        /// <summary>
        /// 初始的武将牌
        /// </summary>
        public static List<CardBaseTemplate> OriginalBossTemplates { get; private set; }

        /// <summary>
        /// 初始的身份牌
        /// </summary>
        public static List<CardBaseTemplate> OriginalIdentityTemplates { get; private set; }
    }

    /// <summary>
    /// 牌堆分类
    /// </summary>
    public enum CardDeckType
    {
        None,
        /// <summary>
        /// 使用的牌，可以使用的牌（基本牌、锦囊牌、装备牌）
        /// </summary>
        CommonDeck,
        /// <summary>
        /// 身份牌
        /// </summary>
        IdentityDeck,
        /// <summary>
        /// 武将牌
        /// </summary>
        BossDeck,
    }
}
